using System;
using System.Collections.Generic;
using BitMango.Diagnostics;
using UnityEngine;

[RequireComponent(typeof(InputManager))]
public class InputPatternManager : SingletonMonoBehaviour<InputPatternManager>
{
	private void Start()
	{
		if (null == this.viewCamera)
		{
			this.viewCamera = Camera.main;
		}
		InputManager.onMouseDown = (Action<Vector3>)Delegate.Combine(InputManager.onMouseDown, new Action<Vector3>(this.OnMouseDown));
		InputManager.onMouseDrag = (Action<Vector3>)Delegate.Combine(InputManager.onMouseDrag, new Action<Vector3>(this.OnMouseDrag));
		InputManager.onMouseUp = (Action<Vector3>)Delegate.Combine(InputManager.onMouseUp, new Action<Vector3>(this.OnMouseUp));
	}

	public static void RegisterPattern(string pattern, Action callback)
	{
		Debugger.Assert(!string.IsNullOrEmpty(pattern));
		Debugger.Assert(callback != null);
		if (string.IsNullOrEmpty(pattern))
		{
			return;
		}
		if (callback == null)
		{
			return;
		}
		if (SingletonMonoBehaviour<InputPatternManager>.Instance.map.ContainsKey(pattern))
		{
			return;
		}
		SingletonMonoBehaviour<InputPatternManager>.Instance.map.Add(pattern, callback);
	}

	public static void UnRegisterPattern(string pattern, Action callback)
	{
		Debugger.Assert(!string.IsNullOrEmpty(pattern));
		Debugger.Assert(callback != null);
		if (string.IsNullOrEmpty(pattern))
		{
			return;
		}
		if (callback == null)
		{
			return;
		}
		if (!SingletonMonoBehaviour<InputPatternManager>.Instance.map.ContainsKey(pattern))
		{
			return;
		}
		if (SingletonMonoBehaviour<InputPatternManager>.Instance.map[pattern] != callback)
		{
			return;
		}
		SingletonMonoBehaviour<InputPatternManager>.Instance.map.Remove(pattern);
	}

	private void OnMouseDown(Vector3 mousePosition)
	{
		this.patternIndex = this.PatternIndex(mousePosition);
		if (this.patternIndex < 0)
		{
			return;
		}
		this.pattern = this.patternIndex.ToString();
	}

	private void OnMouseDrag(Vector3 mousePosition)
	{
		if (this.patternIndex < 0)
		{
			return;
		}
		int num = this.PatternIndex(mousePosition);
		if (num < 0 || num == this.patternIndex)
		{
			return;
		}
		this.patternIndex = num;
		this.pattern += this.patternIndex.ToString();
	}

	private void OnMouseUp(Vector3 mousePosition)
	{
		if (this.patternIndex < 0)
		{
			return;
		}
		int num = this.PatternIndex(mousePosition);
		if (num < 0)
		{
			return;
		}
		if (this.map.ContainsKey(this.pattern))
		{
			Action action = this.map[this.pattern];
			action();
		}
	}

	private int PatternIndex(Vector3 mousePosition)
	{
		Vector3 b = this.viewCamera.ScreenToWorldPoint(mousePosition) - this.viewCamera.transform.position;
		b.z = 0f;
		float num = this.viewCamera.orthographicSize * this.viewCamera.aspect;
		float orthographicSize = this.viewCamera.orthographicSize;
		int num2;
		if (b.x < -num / 3f)
		{
			num2 = -1;
		}
		else if (b.x < num / 3f)
		{
			num2 = 0;
		}
		else
		{
			num2 = 1;
		}
		int num3;
		if (b.y < -orthographicSize / 3f)
		{
			num3 = -1;
		}
		else if (b.y < orthographicSize / 3f)
		{
			num3 = 0;
		}
		else
		{
			num3 = 1;
		}
		Vector3 a = new Vector3((float)num2 * num * 2f / 3f, (float)num3 * orthographicSize * 2f / 3f, 0f);
		if ((a - b).magnitude < (num + orthographicSize) / 10f)
		{
			return (num3 + 1) * 3 + (num2 + 1) + 1;
		}
		return -1;
	}

	public Camera viewCamera;

	private int patternIndex = -1;

	private string pattern = string.Empty;

	private Dictionary<string, Action> map = new Dictionary<string, Action>();
}
